var href = window.location.href;
var _term_id = '';
var _id = '';
$('document').ready(function(){      
    $('.control-group .set_menu').click(function(){
       var checked = $('.control-group .set_menu:checked').length;
       if(checked){$('.activex').css('display','block');}
       else{$('.activex').css('display','none');}
    });
    
    //change config mail
    $('#ConfigMailTypeMail').live('change',function(){     
        var _type_mail = $(this).val(); 
        config_mail(_type_mail)  
    });
    //End
    
    //load list email 
    $('#PaginateGroupUser').live('change',function(){
        var _term_id = $(this).val();        
        load_email_register(_term_id);
        
    });
    //end  
    //load config mail
        var _type_mail = $('#ConfigMailTypeMail').val();
        config_mail(_type_mail);
    //end
    //for fancybox move info
    $("a#fcbShare").fancybox({                
        "width":500,
        "height":500
    });      
    //end
    //change select style block
    $('#BlockStyleBlock').live('change',function(){
        var _val = $(this).val();
        if(_val!=''){
            $('.input-delta').css('display','none');   
        }else{
            $('.input-delta').css('display','block');
        }        
    });
    //end
    //change role subsite
    $('#RoleSubsite').live('change',function(){
        var _val = $(this).val();        
        var _role_id = $('#hd_role_id').val(); 
        load_node_type(_val,_role_id);         
    });
    //End
    //change kind node type
    $('#NodeTypeKindNodeTypeId').live('change',function(){        
        var _val = $(this).val(); 
        _id = $('#hd-get-id-node-type').val();                      
        load_field(_val,_id);         
    });
    //End
    //Change theme in backend  
    /*$('#BlockThemeId').live('change',function(){ 
        var _theme_id = $(this).val();
        load_style_block(_theme_id);        
    })  */
    //End          
});
//load style block belongs theme
function load_style_block(_theme_id){   
   $('.wd-image-loader').css('position','absolute');
   $('.wd-image-loader').css('display','block');   
   $('.wd-image-loader').css('opacity','1.0');
   $('.panel').css('opacity','0.4');   
   $.ajax({
        url: "/block/block/load_style_block/"+_theme_id                  
    }).done(function(data) {        
        $('.wd-image-loader').css('display','none');
        $('.panel').css('opacity','2.0');         
        $('#BlockStyleBlock').html(data);        
    }); 
}
//End
//add important
function add_important(id){ 
    var important;   
    if($('.input_importamt_'+id).is(':checked')){
       important = 1;   
    }else{
       important = 0; 
    }
    $.ajax({
        url: "/node/node/add_important_contact/"+id+'/'+important                  
    }).done(function(data) {        
                  
    });
}
//End
//add favourite
function add_favourite(id){
   var favourite;
   if($('.input_favourite_'+id).is(':checked')){
        favourite = 1;        
    }else{
        favourite = 0; 
    }
    $.ajax({
        url: "/node/node/add_favourite_contact/"+id+'/'+favourite                  
    }).done(function(data) {        
                     
    }); 
}
//End
//process init config mail
function load_node_type(_val,_role_id){
    /*$('.wd-image-loader').css('display','block');
    $('#wrap-chk-node-type').css('opacity','0.5');
    $('.wd-image-loader').css('opacity','10');    */
    $.ajax({
        url: "/node/node/load_node_type/"+_val+'/'+_role_id                   
    }).done(function(data) {        
        $('#wrap-chk-node-type').html(data);
        /*$('.wd-image-loader').css('display','none');
        $('#wrap-chk-node-type').css('opacity','10');
        $('.wd-image-loader').css('opacity','0.5'); */       
    }); 
}
function load_field(_val,id){   
    $.ajax({
        url: "/node/node/load_field/"+_val+'/'+id                   
    }).done(function(data) {               
        $('.list-chk-node-type').html(data);               
    }); 
}
function config_mail(_type_mail){    
    if(_type_mail==1){
        $('.change_type_mail').css('display','block');
    }else{
        $('.change_type_mail').css('display','none'); 
    }  
}
function load_email_register(term_id){
   $.ajax({
        url: "/node/node/load_email_register/"+term_id                   
    }).done(function(data) {        
        $('.list-user-email').html(data);                  
    }); 
}
function add_mail_user(email,id){    
    if($('._lst_email_'+id).is(':checked')){       
        if(checkDuplicate(id))
        {            
            msg = 'Email này đã được chọn';        
            alert(msg);
            return false; 
        }
        else
        {
             var _email = '';
            _email += "<li class=mail_"+id+">";
            _email += "<input type=hidden"; 
            _email += " name=data[Node][email][]";       
            _email += " value="+email;
            _email += " class=_lst_email_"+id;
            _email += "> ";
            _email += email;
            _email += "<a href='#' onclick=_delete_checked_mail('"+id+"')>";
            _email += " Delete";
            _email += "</a>";
            _email += "</li>";
            $('.list-email-checked').append(_email); 
        }          
    }    
}
function _delete_checked_mail(id){
    $('.list-email-checked li.mail_'+id).remove(); 
    return false;   
}
function checkDuplicate(id)
{
   var check = false;
   $(".list-email-checked li").each(function()
  {
     if($(this).hasClass('mail_'+id))
     {
         check = true;
     }
  });
  return check;  
}
function set_status(id,status){
   $.ajax({
        url: "/node/node/set_status/"+id+"/"+status                   
    }).done(function(data) {
        $('.link-control-'+id).html(data);
        alert('Xong');                
    }); 
}
function delete_comment(id){
   $.ajax({
        url: "/node/node/delete_comment/"+id                   
    }).done(function(data) { 
        $('._comment_'+id).remove();       
        alert('Xong');                
    }); 
}
